<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<view class="content detail">
			
			<!--退款信息-->
			<view class="detail-item">
				<uni-section title="退款信息" type="line"></uni-section>
				<view class="detail-content">
					<view class="detail-text">
						退款单号：
						<text>{{info.number}}</text>
						<text class="detail-text-copy" @click="copyText(info.number)">复制</text>
					</view>
					<view class="detail-text">
						退款状态：
						<uni-tag size="small" :text="arrText(orderRefundStatusList, info.status)" />
					</view>
					<view class="detail-text" v-if="info?.status === 5">
						付款业务编号：
						<text>{{info.tradeNo}}</text>
						<text class="detail-text-copy" @click="copyText(info.tradeNo)">复制</text>
					</view>
					<view class="main-flex detail-text detail-flex-start" v-if="info?.status === 5">
						退款凭证：
						<view class="detail-text_r" @click.stop="previewImg(info.refundImg)">
							<up-image width="120rpx" height="120rpx" :src="info.refundImg" :lazy-load="true"></up-image>
						</view>
					</view>
					<view class="detail-text">
						仓库流水：
						<text>{{info.orderNumber}}</text>
						<text class="detail-text-copy" @click="copyText(info.orderNumber)">复制</text>
						<text v-if="checkPermi(['zhouyz:warehouseRecord:list'])" class="detail-text-copy" @click="showOrder(`/pages/warehouse/record?number=${info.number}`)">查看</text>
					</view>
					<view class="detail-text">
						退款说明：
						<text>{{info.remark}}</text>
					</view>
				</view>
			</view>
			
			<!--订单信息-->
			<view class="detail-item">
				<uni-section title="订单信息" type="line"></uni-section>
				<view class="detail-content">
					<view class="detail-text">
						订单编号：
						<text>{{orderInfo.orderNumber}}</text>
						<text class="detail-text-copy" @click="copyText(orderInfo.orderNumber)">复制</text>
						<text v-if="checkPermi(['zhouyz:order:list'])" class="detail-text-copy" @click="showOrder(`/pages/order/orderDetail?id=${info.orderId}`)">查看</text>
					</view>
					<view class="detail-text">
						订单状态：
						<uni-tag size="small":class="`order-status-${orderInfo.status}`" :text="arrText(allStatusList, orderInfo.status)" />
					</view>
					<view class="detail-text">
						发货时间：
						<text>{{orderInfo.sendTime}}</text>
					</view>
					<view class="detail-text">
						用户姓名：
						<text>{{orderInfo.customerName}}</text>
						<text class="detail-text-copy" @click="copyText(orderInfo.customerName)">复制</text>
					</view>
					<view class="detail-text">
						注册电话：
						<text>{{orderInfo.accountPhone}}</text>
						<text class="detail-text-copy" @click="copyText(orderInfo.accountPhone)">复制</text>
						<text class="detail-text-copy" @click="call(orderInfo.accountPhone)">拨打</text>
					</view>
					<view class="detail-text">客户店铺：<text>{{orderInfo.company}}</text></view>
					<view class="detail-text">销售人员：<text>{{orderInfo.salesmanName}}</text></view>
					<view class="detail-text">下单时间：<text>{{orderInfo.createTime}}</text></view>
					<view class="detail-text">付款时间：<text>{{orderInfo.payTime}}</text></view>
					<view class="detail-text">订单备注：<text>{{orderInfo.remark}}</text></view>
					<view class="detail-text">配送仓库：<text>{{orderInfo.warehouseName}}</text></view>
					<view class="detail-text">应付金额：<text>{{orderInfo.originalPrice}}</text></view>
					<view class="detail-text">实付金额：<text>{{orderInfo.actualPrice}}</text></view>
					<view class="detail-text">折扣金额：<text>{{orderInfo.discountPrice}}</text></view>
					<view class="detail-text">已退款金额：<text style="color: red;">{{orderInfo.refundPrice}}</text></view>
					<view class="detail-text">本次退款金额：<text style="color: green;">{{refundTotal}}</text></view>
					<view class="detail-text">
						退款方式：
						<uni-tag size="small" :text="info.refundType === 1 ? '线下' : '线上'" />
					</view>
				</view>
			</view>
			<!--商品清单-->
			<view class="detail-item" v-if="checkPermi(['zhouyz:orderRefundDetail:list'])">
				<uni-section title="商品清单" type="line"></uni-section>
				<view class="detail-tip">
					<view class="detail-tip-item">1、只有“退货退款”方式才会影响库存；</view>
					<view class="detail-tip-item">2、退款金额=退货整件*退货单价；</view>
					<view class="detail-tip-item">3、订单未发货退款方式只能选择仅退款和退补差价；</view>
					<view class="detail-tip-item">4、发货退款方式选择仅退款和退补差价，退货整件只能填0(收货状态缺货未发货除外)，退款金额就是退货单价；</view>
					<view class="detail-tip-item">5、收货状态缺货未发货，退货整件要填；</view>
				</view>
				<view class="detail-content table">
					<ty-table
						:data="tableList"
						:columns="columns"
						:sortable="true"
						:cellClassName="() => 'table-cell'"
						:headerCellClassName="() => 'table-cell'"
						:rowClassName="() => 'table-row'"
						stripe
						border>
						<template v-slot:left="{ row }">
							<view class="table-td-cell">
								<view class="table-td-row">
									商品：
									<text class="table-td-text">{{row.productName}}</text>
								</view>
								<view class="table-td-row">
									批次：
									<text class="table-td-text">{{row?.batchs || ''}}</text>
								</view>
								<view class="table-td-row">
									单价：
									<text class="table-td-text">{{row.productPrice}}</text>
								</view>
								<view class="table-td-row">
									单位：
									<text class="table-td-text">{{row.productUnit}}</text>
								</view>
								<view class="table-td-row">
									数量：
									<text class="table-td-text">{{row.productNumber}}</text>
								</view>
								<view class="table-td-row">
									收货状态：
									<uni-tag size="small" :text="arrText(receiveStatusList, row.receiveStatus)" />
								</view>
								<view class="table-td-row">
									退款方式：
									<uni-tag size="small" :text="arrText(refundTypeList, row.refundType)" />
								</view>
								<view class="table-td-row">
									退款原因：
									<uni-tag size="small" :text="arrText(orderRefundReasonList, row.reasonType)" />
								</view>
							</view>
						</template>
						<template v-slot:right="{ row }">
							<view class="table-td-cell">
								<view class="table-td-row">
									退货整件：
									<text class="table-td-text">{{row.refundNumber}}</text>
								</view>
								<view class="table-td-row">
									退货单价：
									<text class="table-td-text">{{row.refundPrice}}</text>
								</view>
								<view class="table-td-row">
									退款金额：
									<text class="table-td-text">{{row.refundTotal}}</text>
								</view>
								<view class="table-td-row">
									退零数量：
									<text class="table-td-text">{{row.refundPartNumber}}</text>
								</view>
								<view class="table-td-row">
									浪费整件：
									<text class="table-td-text">{{row.wasteNumber}}</text>
								</view>
								<view class="table-td-row">
									浪费零散：
									<text class="table-td-text">{{row.wastePartNumber}}</text>
								</view>
								<view class="table-td-row">
									备注：
									<text class="table-td-text">{{row.remark}}</text>
								</view>
							</view>
						</template>
						<template v-slot:after="{ row }">
							<view class="table-td-after table-cell table-td-row">
								<view class="table-td-row table-td-operate main-flex">
									<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:orderRefundDetail:edit'])">
										<up-button
											size="mini"
											type="primary"
											@click.stop="editFun(row)">
											<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
											<view class="table-operate-btn-text">修改</view>
										</up-button>
									</view>
									<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:orderRefundDetail:remove'])">
										<up-button
											size="mini"
											type="error"
											@click.stop="deleteFun(row)">
											<uni-icons class="table-operate-btn-icon" type="trash" size="24rpx" color="#ffffff"></uni-icons>
											<view class="table-operate-btn-text">删除</view>
										</up-button>
									</view>
								</view>
							</view>
						</template>
					</ty-table>
				</view>
			</view>
			
			
			<!--退款说明-->
			<view class="detail-item">
				<uni-section title="退款说明" type="line"></uni-section>
				<view class="detail-content">
					<view class="detail-text">
						<uni-forms
							ref="orderRef"
							:modelValue="orderModel"
							label-width="160rpx">
							<uni-forms-item label="退款说明" name="remark">
								<uni-easyinput type="textarea" v-model="orderModel.remark" placeholder="请输入退款说明"></uni-easyinput>
							</uni-forms-item>
						</uni-forms>
					</view>
				</view>
			</view>
			
			<!--操作-->
			<view class="detail-btns main-flex" v-if="info.id">
				<view v-if="checkPermi(['zhouyz:orderRefund:edit']) && info?.status === 0" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="primary"
						@click.stop="orderRefundAddEditFun(1)">
						<view class="table-operate-btn-text">提交</view>
					</up-button>
				</view>
				<!-- <view v-if="checkPermi(['zhouyz:orderRefund:storage']) && info?.status === 2" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="primary"
						@click.stop="orderRefundAddEditFun(3)">
						<view class="table-operate-btn-text">确认入库</view>
					</up-button>
				</view> -->
				<view v-if="checkPermi(['zhouyz:orderRefund:audit']) && info?.status === 2" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="primary"
						@click.stop="orderRefundAddEditFun(2)">
						<view class="table-operate-btn-text">确认无误</view>
					</up-button>
				</view>
				<view v-if="checkPermi(['zhouyz:orderRefund:return']) && info?.status > 1 && info?.status < 5" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="error"
						@click.stop="orderRefundAddEditFun(0)">
						<view class="table-operate-btn-text">退回重填</view>
					</up-button>
				</view>
				<view v-if="checkPermi(['zhouyz:orderRefund:payment']) && info?.status === 4" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="primary"
						@click.stop="orderRefundAddEditFun(4)">
						<view class="table-operate-btn-text">线上退款</view>
					</up-button>
				</view>
				<view v-if="checkPermi(['zhouyz:orderRefund:payment']) && info?.status === 4" class="detail-btn main-flex">
					<up-button
						class="detail-btn-info"
						type="primary"
						@click.stop="orderRefundAddEditFun(5)">
						<view class="table-operate-btn-text">线下退款</view>
					</up-button>
				</view>
			</view>
		</view>
		
		
		
		<!--修改商品弹窗-->
		<up-popup :z-index="90" round="5" closeable :show="showEdit" @close="editCancel" mode="center">
			<view class="edit">
				<view class="edit-header">退款商品信息填写</view>
				<scroll-view scroll-y class="edit-box">
					<view class="edit-form" v-if="editModel.id">
						<uni-forms
							ref="editRef"
							:rules="editRules"
							:modelValue="editModel"
							err-show-type="toast"
							label-width="160rpx">
							<uni-forms-item label="商品名称">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.productName">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="单价">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.productPrice">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="单位">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.productUnit">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="退货批次">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.batchs">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="数量">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.productNumber">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="退款方式" required>
								<uni-data-select
									v-model="editModel.refundType"
									placeholder="请选择退款方式"
									@change="setRefundNumber"
									:localdata="refundTypeListArr"></uni-data-select>
							</uni-forms-item>
							<uni-forms-item label="收货状态" required>
								<uni-data-select
									v-model="editModel.receiveStatus"
									placeholder="请选择收货状态"
									@change="setRefundNumber"
									:localdata="receiveStatusList"></uni-data-select>
							</uni-forms-item>
							<uni-forms-item label="退货整件" name="refundNumber">
								<uni-easyinput @blur="setRefundNumber" type="number" v-model="editModel.refundNumber" placeholder="请输入退货整件数量"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="退货单价" name="refundPrice" required>
								<uni-easyinput type="number" v-model="editModel.refundPrice" placeholder="请输入退货单价"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="退款金额">
								<uni-easyinput
									disabled
									type="text"
									v-model="editModel.refundTotal">
								</uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="退款原因">
								<uni-data-select
									v-model="editModel.reasonType"
									placeholder="请选择退款原因"
									:localdata="orderRefundReasonList"></uni-data-select>
							</uni-forms-item>
							<uni-forms-item label="退零数量" name="refundPartNumber">
								<uni-easyinput type="number" v-model="editModel.refundPartNumber" placeholder="请输入退零数量"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="浪费整件" name="wasteNumber">
								<uni-easyinput type="number" v-model="editModel.wasteNumber" placeholder="请输入浪费整件数量"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="浪费零散" name="wastePartNumber">
								<uni-easyinput type="number" v-model="editModel.wastePartNumber" placeholder="请输入浪费零散数量"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="备注" name="remark">
								<uni-easyinput type="textarea" v-model="editModel.remark" placeholder="请输入备注"></uni-easyinput>
							</uni-forms-item>
						</uni-forms>
						<view class="edit-footer main-flex">
							<up-button
								class="edit-footer-item"
								@click="editCancel"
								text="取消"></up-button>
							<up-button
								class="edit-footer-item"
								text="确认"
								type="primary"
								:disabled="isSubmit"
								:loading="isSubmit"
								@click="editConfirm"></up-button>
						</view>
					</view>
				</scroll-view>
				
			</view>
		</up-popup>
		
		<!--线下退款-->
		<up-popup :z-index="90" round="5" closeable :show="showBack" @close="backCancel" mode="center">
			<view class="edit">
				<view class="edit-header">线下退款</view>
				<view class="edit-form">
					<uni-forms
						ref="backRef"
						:modelValue="backModel"
						label-width="160rpx">
						<uni-forms-item label="付款业务编号" name="tradeNo">
						  <uni-easyinput v-model="backModel.tradeNo" placeholder="请输入付款业务编号" />
						</uni-forms-item>
						<uni-forms-item label="付款截图" name="refundImg">
							<up-upload
								:fileList="fileList"
								@afterRead="afterRead"
								@delete="deletePic"
								:maxCount="1"
								></up-upload>
						  <uni-easyinput style="display: none;" v-model="backModel.refundImg" placeholder="上传付款截图" />
						</uni-forms-item>
					</uni-forms>
					<view class="edit-footer main-flex">
						<up-button
							class="edit-footer-item"
							@click="backCancel"
							text="取消"></up-button>
						<up-button
							class="edit-footer-item"
							text="确认"
							type="primary"
							:disabled="isAdd"
							:loading="isAdd"
							@click="orderRefundStatusFun(5)"></up-button>
					</view>
				</view>
			</view>
		</up-popup>
		
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref, watch } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { allProvince } from "@/api/erp_api/region"
import { orderStatus, orderRefundReason } from "@/api/system/dict/data"
import { 
	orderRefundProduct,
	orderDetail,
	orderRefundDetail,
	orderRefundProductDelete,
	orderRefundSubmit,
	orderRefundEdit,
	orderRefundOfflinePayment,
	orderRefundProductUpdate } from "@/api/erp_api/order"
import { copyText, arrText, previewImg } from '@/utils/ty.js';
import { orderRefundStatusList, receiveStatusList, refundTypeList } from '@/utils/config.js';
import useUserStore from '@/store/modules/user'
import { Decimal } from 'decimal.js'
import { test } from 'uview-plus'
import { upload } from "@/api/system/common"

	const userStore = useUserStore()
	const refundTypeListArr = ref(refundTypeList)
	onLoad(async (option) => {
		if (option.id) {
			await getOrderRefundReason()
			await getOrderStatus()
			id.value = option.id
			getDetail()
			if (checkPermi(['zhouyz:orderRefundDetail:list'])) {
				getProductDetail()
			}
		}
	})
	
	// 获取订单退款原因
	const orderRefundReasonList = ref([])
	const getOrderRefundReason = () => {
		return orderRefundReason().then(res => {
			orderRefundReasonList.value = res.data.map(item => {
				return {
					...item,
					value: Number(item.dictValue),
					text: item.dictLabel
				}
			}) || []
		})
	}
	
	// 获取订单状态
	const allStatusList = ref([])
	const getOrderStatus = () => {
		return orderStatus().then(res => {
			allStatusList.value = res.data.map(item => {
				return {
					...item,
					value: item.dictValue,
					text: item.dictLabel
				}
			}) || []
		})
	}
	
	// 数据查询
	const id = ref('')
	const info = ref({})
	const tableList = ref([])
	const isLoading = ref(false)
	
	const getDetail = () => {
		const params = {
			id: id.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		orderRefundDetail(params).then(res => {
			info.value = res.data || {}
			orderModel.value.remark = res.data.remark
			getOrderDetail(res.data.orderId)
			isLoading.value = false
			uni.hideLoading()
	  }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	const orderInfo = ref({})
	const getOrderDetail = (orderId) => {
		const params = {
			id: orderId
		}
		orderDetail(params).then(res => {
			orderInfo.value = res.data || {}
	  })
	}
	
	// 表格头部
	const columns = ref([
		{ title: '商品信息', key: 'left' },
		{ title: '退款信息', key: 'right', width: '320rpx' }
	])
	
	const getProductDetail = () => {
		const params = {
			orderRefundId: id.value
		}
		orderRefundProduct(params).then(res => {
			tableList.value = res.rows || []
    })
	}
	
	// 查看
	const showOrder = (url) => {
		uni.navigateTo({
			url
		})
	}
	
	// 拨打电话
	const call = (phone) => {
		uni.makePhoneCall({
			phoneNumber: phone
		})
	}
	
	const refundTotal = computed(() => {
		return tableList.value.reduce((acc, curr) => {
			return Decimal(acc).add(curr.refundTotal).toNumber()
		}, 0)
	})
	
	// 删除商品清单
	const deleteFun = (row) => {
		orderRefundProductDelete({
			id: row.id
		}).then(res => {
			getProductDetail()
		})
	}
	
	
	// 修改
	const showEdit = ref(false)
	const editRef = ref(null)
	const isSubmit = ref(false)
	const editModel = ref({})
	const editRules = ref({
		refundType: {
			rules: [{ required: true, errorMessage: '请选择退款方式' }]
		},
		receiveStatus: {
			rules: [{ required: true, errorMessage: '请选择收货状态' }]
		},
		refundPrice: {
			rules: [{ required: true, errorMessage: '请输入退货单价' }]
		}
	})
	//打开退款
	const editFun = (row) => {
		if (row.batchs) {
			refundTypeListArr.value = refundTypeList
		} else {
			refundTypeListArr.value = [
				{ value: 1, text: '仅退款'},
				{ value: 2, text: '补(退)差价'}
			]
		}
		editModel.value = {...row}
		const refundType = refundTypeListArr.value.find(item => item.value == row.refundType)
		if (!refundType) {
			editModel.value.refundType = ''
		}
		showEdit.value = true
	}
	
	// 取消退款
	const editCancel = () => {
		showEdit.value = false
		isSubmit.value = false
		editModel.value = {}
	}
	
	const editConfirm = () => {
		editRef.value.validate().then(formRes => {
			const params = {
				...editModel.value
			}
			if (!test.digits(params.refundType)) {
				modal.showToast('请选择退款方式')
				return false
			}
			if (!test.digits(params.receiveStatus)) {
				modal.showToast('请选择收货状态')
				return false
			}
			if (!test.number(params.refundPrice)) {
				modal.showToast('请输入退货单价')
				return false
			}
			isSubmit.value = true
			orderRefundProductUpdate(params).then((res) => {
				getProductDetail()
				editCancel()
				modal.showToast(res.msg)
			}).catch(err => {
				modal.showToast(err)
				isSubmit.value = false
			})
		})
	}
	
	// // 退货整件数量
	// const blurRefundNumber = (e) => {
	// 	setRefundNumber()
	// }
	// // 退款方式选择
	// const changeRefundType = (e) => {
	// 	setRefundNumber()
	// }
	// // 退款方式选择
	// const changeRefundType = (e) => {
	// 	setRefundNumber()
	// }
	// 退货整件数量计算
	const setRefundNumber = () => {
		if ((editModel.value.refundType == 1 || editModel.value.refundType == 2) && editModel.value.receiveStatus != 2) {
			editModel.value.refundNumber = 0
		}
		setRefundTotal()
	}
	
	// watch(() => editModel.value.refundNumber, (nval, oval) => {
	// 	setRefundTotal()
	// })
	watch(() => editModel.value.refundPrice, (nval, oval) => {
		setRefundTotal()
	})
	
	const setRefundTotal = () => {
		if (!editModel.value.refundNumber || !editModel.value.refundPrice) {
			if((editModel.value.refundType === 1 || editModel.value.refundType === 2) && editModel.value.receiveStatus != 2){
				editModel.value.refundTotal = editModel.value.refundPrice ? parseFloat(editModel.value.refundPrice) : 0
			} else {
				editModel.value.refundTotal = 0
			}
			return false
		}
		editModel.value.refundTotal = Decimal(editModel.value.refundNumber).mul(editModel.value.refundPrice).toNumber()
	}
	
	const isAdd = ref(false)
	const orderRef = ref(null)
	const orderModel = ref({
		remark: ''
	})
	const orderRefundAddEditFun = (status) => {
		orderRef.value.validate().then(formRes => {
			const params = {...info.value}
			if (checkPermi(['zhouyz:orderRefundDetail:edit'])) {
				params.refundCurrent = parseFloat(refundTotal.value)
			}
			params.remark = orderModel.value.remark
			if (parseFloat(orderInfo.value.actualPrice) < params.refundCurrent) return modal.showToast('退款金额超过实付款金额')
			isAdd.value = true
			orderRefundEdit(params).then(res => {
				if (res.code === 200) {
					if (status === 5) {
						isAdd.value = false
						showBack.value = true
					} else {
						orderRefundStatusFun(status)
					}
				} else {
					modal.showToast(res.msg)
					isAdd.value = false
				}
			}).catch((err) => {
				modal.showToast(err)
				isAdd.value = false
			})
		})
	}
	const orderRefundStatusFun = (status) => {
		if (status === 5) { // 线下付款
			isAdd.value = true
			backRef.value.validate().then(formRes => {
				const params = {
					...info.value,
					refundImg: formRes.refundImg,
					tradeNo: formRes.tradeNo
				}
				orderRefundOfflinePayment(params).then(res => {
					modal.showToast('操作成功')
					getDetail()
					if (checkPermi(['zhouyz:orderRefundDetail:list'])) {
						getProductDetail()
					}
					uni.$emit('refreshOrderList');  // 触发刷新事件
					isAdd.value = false
					showBack.value = false
				}).catch((err) => {
					modal.showToast(err)
					isAdd.value = false
					showBack.value = false
				})
			})
		} else {
			orderRefundSubmit({
				remark: ''
			}, id.value, status).then(res => {
				getDetail()
				if (checkPermi(['zhouyz:orderRefundDetail:list'])) {
					getProductDetail()
				}
				uni.$emit('refreshOrderList');  // 触发刷新事件
				modal.showToast('操作成功')
				isAdd.value = false
			}).catch((err) => {
				modal.showToast(err)
				isAdd.value = false
			})
		}
	}
	const backCancel = () => {
		showBack.value = false
		fileList.value = []
		backModel.value.refundImg = ''
		backModel.value.tradeNo = ''
	}
	
	const showBack = ref(false)
	const backRef = ref(null)
	const backModel = ref({
		refundImg: '',
		tradeNo: ''
	})
	// 图片上传
	const fileList = ref([])
	
	// 删除图片
	const deletePic = (event) => {
	  fileList.value.splice(event.index, 1)
		backModel.value.refundImg = fileList.value.map(item => item.url).join(',')
	}
	
	// 新增图片
	const afterRead = async (event) => {
	  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
	  let lists = [].concat(event.file)
	  let fileListLen = fileList.value.length
	  lists.map((item) => {
	    fileList.value.push({
	      ...item,
	      status: 'uploading',
	      message: '上传中',
	    });
	  });
	  for (let i = 0; i < lists.length; i++) {
	    const result = await upload(lists[i].url)
			const data = JSON.parse(result.data)
	    let item = fileList.value[fileListLen]
	    fileList.value.splice(fileListLen, 1, {
	      ...item,
	      status: 'success',
	      message: '',
	      url: data.url,
	    });
	    fileListLen++;
	  }
		backModel.value.refundImg = fileList.value.map(item => item.url).join(',')
	};
	
	// const computeSumRefundPrice = () => {
	// 	let sumPrice = 0;
	// 	const orderProductList = tableList.value
	// 	for (let i = 0; i < orderProductList.length; i++) {
	// 		const item = orderProductList[i]
	// 		const refundType = Number(item.refundType)
	// 		if(refundType===0){
	// 			item.refundCurrent = Decimal(item.refundNumber).mul(item.refundPrice).toNumber()
	// 		}
	// 		else if(refundType === 1 || refundType === 2){
	// 			if(item.receiveStatus != 2) {
	// 				item.refundNumber = 0
	// 				item.refundCurrent = parseFloat(item.refundPrice)
	// 			}else{
	// 				item.refundCurrent = Decimal(item.refundNumber).mul(item.refundPrice).toNumber()
	// 			}
	// 		}
	// 		item.refundTotal = item.refundCurrent
	// 		sumPrice = sumPrice + item.refundCurrent
	// 	}
	// 	console.log(sumPrice)
	// }
	
</script>

<style scoped>
.detail {
	padding-bottom: 60rpx;
}
.edit-box {
	max-height: 60vh;
	padding: 0 20rpx;
}
</style>
